// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import App from './App';
import AppMgr from './manager/AppMgr';
import LocalStore from './utils/LocalStore';
import Setting from './config/Setting';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export async function activate(context: vscode.ExtensionContext) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, fishReader is now active!');
	await LocalStore.init();
	// Setting.init();
	// AppMgr.init();


	vscode.workspace.onDidChangeConfiguration((event) => {
		if (event.affectsConfiguration("fish.setReadModel")) {
			App.exit();
			return;
		}

		const _cmdArr = [
			"fish.setLineChar",
			"fish.setPageLine"
		];	
		const _isChange = _cmdArr.some(cmd => event.affectsConfiguration(cmd));
		if (_isChange) {
			Setting.init();
		}
	});

	// let _cfg: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration();
	// let _d1 = _cfg.update("fish.setLineChar", 90, true);

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	let startCmd = vscode.commands.registerCommand('fish.start', () => {
		Setting.init();
		AppMgr.init();
		App.start();

		// let _edt = vscode.window.activeTextEditor;
		// _edt?.edit((builder) => {
		// 	builder.replace(
		// 		new vscode.Range(new vscode.Position(1, 0), new vscode.Position(2, 0)),
		// 		"var cba = 123;\n"
		// 	);
		// });
	});
	context.subscriptions.push(startCmd);

	let exitCmd = vscode.commands.registerCommand('fish.exit', () => {
		App.exit();
	});
	context.subscriptions.push(exitCmd);

	let nextCmd = vscode.commands.registerCommand('fish.nextPage', () => {
		App.toNext();
	});
	context.subscriptions.push(nextCmd);

	let lastCmd = vscode.commands.registerCommand('fish.lastPage', () => {
		App.toLast();
	});
	context.subscriptions.push(lastCmd);

	let bossCmd = vscode.commands.registerCommand('fish.boss', () => {
		App.switchBoss();
	});
	context.subscriptions.push(bossCmd);

	let jumpCmd = vscode.commands.registerCommand('fish.jump', () => {
		App.jumpTo();
	});
	context.subscriptions.push(jumpCmd);

	// let settingCmd = vscode.commands.registerCommand('fish.setting', () => {
	// 	App.showSetting();
	// });
	// context.subscriptions.push(settingCmd);

	// let setPageLine = vscode.commands.registerTextEditorCommand('fish.setPageLine', () => {
	// 	console.log("setPageLine");
	// });
	// context.subscriptions.push(setPageLine);
}

// This method is called when your extension is deactivated
export function deactivate() {
	
}


